package exttable.testversioning.tablemodel;

import dbutils.DbUtils;
import dbutils.StoredQueries;
import exttable.view.ViewAnalysisParameter;
import exttable.view.ViewUnits;
import utils.Localisation;

import javax.swing.table.AbstractTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jun 8, 2010
 * Time: 4:45:22 PM
 * To change this template use File | Settings | File Templates.
 */
public class AnalysisVersionModel extends AbstractTableModel {
    private int analysisID;
    private ViewUnits unit;
    public static final int PARAMETER_NAME = 0;
    public static final int PARAMETER_PRESENT = 1;
    public static final int PARAMETER_UNIT = 2;
    public static final int MINIMAL_VALUE = 3;
    public static final int MAXIMUM_VALUE = 4;
    public static final int PARAMETER_ORDER = 5;
    private boolean stageFill = true;
    public static final int COLUMN_COUNT = 6;


    private Vector<ViewAnalysisParameter> data;

    public AnalysisVersionModel(int analysisID) {
        this.analysisID = analysisID;
        unit = getUnits().get(0);
        data = getFileds(analysisID);
    }

    public AnalysisVersionModel(Vector<ViewAnalysisParameter> resultData, boolean fill) {
        setData(resultData);
        stageFill = fill;

    }

    public int getAnalysisID() {
        return analysisID;
    }

    public int getRowCount() {
        return data.size();
    }

    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    public Object getValueAt(int row, int column) {
        switch (column) {
            case PARAMETER_NAME:
                return data.get(row);
            case PARAMETER_PRESENT:
                return data.get(row).isPresent();
            case PARAMETER_UNIT:
                return data.get(row).getUnit();
            case MINIMAL_VALUE:
                return data.get(row).getMinValue();
            case MAXIMUM_VALUE:
                return data.get(row).getMaxValue();
            case PARAMETER_ORDER:
                return data.get(row).getOrder();
            default:
                throw new IllegalArgumentException("error");
        }
    }

    public String getColumnName(int column) {
        switch (column) {
            case PARAMETER_NAME:
                return Localisation.getMessage("table.column.prameter");
            case PARAMETER_PRESENT:
                return Localisation.getMessage("table.column.present");
            case PARAMETER_UNIT:
                return Localisation.getMessage("table.column.unit");
            case MINIMAL_VALUE:
                return Localisation.getMessage("table.column.minvalue");
            case MAXIMUM_VALUE:
                return Localisation.getMessage("table.column.maxvalue");
            case PARAMETER_ORDER:
                return Localisation.getMessage("table.column.order");
            default:
                throw new IllegalArgumentException("error");
        }
    }

    public boolean isCellEditable(int row, int column) {
        return stageFill && column != 0;

    }

    public void setValueAt(Object value, int row, int column) {
        switch (column) {
            case PARAMETER_PRESENT:
                data.get(row).setPresent((Boolean) value);
                break;
            case PARAMETER_UNIT:
                data.get(row).setUnit((ViewUnits) value);
                break;
            case MAXIMUM_VALUE:
                data.get(row).setMaxValue((Double) value);
                break;
            case MINIMAL_VALUE:
                data.get(row).setMinValue((Double) value);
                break;
            case PARAMETER_ORDER:
                data.get(row).setOrder((Integer) value);
        }
        fireTableCellUpdated(row, column);

    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case PARAMETER_NAME:
                return String.class;
            case PARAMETER_PRESENT:
                return Boolean.class;
            case PARAMETER_UNIT:
                return String.class;
            case MAXIMUM_VALUE:
                return Double.class;
            case MINIMAL_VALUE:
                return Double.class;
            case PARAMETER_ORDER:
                return Integer.class;
            default:
                throw new IllegalArgumentException("error");
        }
    }

    public Vector<ViewUnits> getUnits() {
        Vector<ViewUnits> toReturn = new Vector<ViewUnits>();
        try {
            Connection c = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = c.prepareStatement(StoredQueries.getQuery(StoredQueries.UNITS_RETRIVE));
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ViewUnits vu = new ViewUnits();
                vu.setUnitKey(rs.getString("unit_key"));
                vu.setNameRu(rs.getString("name_ru"));
                vu.setNameEn(rs.getString("name_en"));
                vu.setNameKz(rs.getString("name_kk"));
                toReturn.add(vu);
            }
            c.close();
            return toReturn;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    private Vector<ViewAnalysisParameter> getFileds(int analysId) {
        Vector<ViewAnalysisParameter> toReturn = new Vector<ViewAnalysisParameter>();

        try {
            Connection c = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = c.prepareStatement(StoredQueries.getQuery(StoredQueries.ANALYSIS_LIMITS));
            ps.setInt(1, analysId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ViewAnalysisParameter vt = new ViewAnalysisParameter.Build()
                        .nameRu(rs.getString("nameRu"))
                        .nameKz(rs.getString("nameKz"))
                        .nameEn(rs.getString("nameEn"))
                        .build();
                vt.setParameterID(rs.getInt("parametrId"));
                vt.setPresent(true);
                vt.setUnit(getUnit(rs.getString("unit")));
                vt.setMinValue(rs.getDouble("minValue"));
                vt.setMaxValue(rs.getDouble("maxValue"));
                vt.setOrder(rs.getInt("parameterOrder"));
                toReturn.add(vt);
            }
            c.close();
            return toReturn;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private ViewUnits getUnit(String unitkey) {
        ViewUnits toReturn = new ViewUnits();
        try {
            Connection c = DbUtils.getDBConnectionGeneral();
            StringBuilder query = new StringBuilder(StoredQueries.getQuery(StoredQueries.UNITS_RETRIVE));
            query.append(" where t1.combination_key = ?");
            PreparedStatement ps = c.prepareStatement(query.toString());
            ps.setString(1, unitkey);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                toReturn.setUnitKey(rs.getString("unit_key"));
                toReturn.setNameRu(rs.getString("name_ru"));
                toReturn.setNameEn(rs.getString("name_en"));
                toReturn.setNameKz(rs.getString("name_kk"));
            }
            c.close();
            return toReturn;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public Vector<ViewAnalysisParameter> getData() {
        return data;
    }

    public void setData(Vector<ViewAnalysisParameter> data) {
        this.data = data;
    }
   
}
